package com.mojiayi.learn.dubbo;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ParseDubboServiceDependencyTest {
    private static String cookie = "";
    private static String authorization = "";

    public static void main(String[] args) {
        String applicationName = "course-dubbo";
        int pageIndex = 0;
        int pageSize = 10;
        while (true) {
            DubboServiceList dubboServiceList = getDubboServiceList(applicationName, pageIndex, pageSize);
            if (dubboServiceList == null) {
                break;
            }
            List<DubboService> dubboServiceContentList = dubboServiceList.getContent();
            for (DubboService dubboService : dubboServiceContentList) {
                List<DubboConsumer> dubboConsumerList = getDubboServiceConsumerList(dubboService.getService(), dubboService.getVersion());
                if (CollectionUtils.isEmpty(dubboConsumerList)) {
                    continue;
                }
                Map<String, List<DubboConsumer>> dubboConsumerMap = dubboConsumerList.stream().collect(Collectors.groupingBy(DubboConsumer::getAppName));
                for (Map.Entry<String, List<DubboConsumer>> entry : dubboConsumerMap.entrySet()) {
                    String str = dubboService.getService();
                    str += "\t";
                    str += entry.getKey();
                    str += "\t";
                    str += entry.getValue().stream().map(DubboConsumer::getIpAddr).distinct().collect(Collectors.joining(","));
                    System.out.println(str);
                }
            }
            pageIndex++;
            if (dubboServiceList.isLast()) {
                break;
            }
        }
    }

    private static DubboServiceList getDubboServiceList(String applicationName, int pageIndex, int pageSize) {
        String url = "https://dubboprod.shiguangkey.com/api/dev/service?pattern=application&filter=" + applicationName + "&page=" + pageIndex + "&size=" + pageSize;
        HttpRequest httpRequest = HttpRequest.get(url);
        httpRequest.header("Cookie", cookie);
        httpRequest.header("Authorization", authorization);
        HttpResponse httpResponse = httpRequest.execute();
        if (httpResponse.getStatus() == HttpStatus.HTTP_OK) {
            Gson gson = new Gson();
            return gson.fromJson(httpResponse.body(), DubboServiceList.class);
        }
        return null;
    }

    private static List<DubboConsumer> getDubboServiceConsumerList(String serviceName, String serviceVersion) {
        String url = "https://dubboprod.shiguangkey.com/api/dev/service/" + serviceName + ":" + serviceVersion;
        HttpRequest httpRequest = HttpRequest.get(url);
        httpRequest.header("Cookie", cookie);
        httpRequest.header("Authorization", authorization);
        HttpResponse httpResponse = httpRequest.execute();
        List<DubboConsumer> dubboConsumerList = new ArrayList<>();
        JsonObject jsonObject = null;
        if (httpResponse.getStatus() == HttpStatus.HTTP_OK) {
            Gson gson = new Gson();
            jsonObject = gson.fromJson(httpResponse.body(), JsonObject.class);
        }
        if (jsonObject == null) {
            return dubboConsumerList;
        }
        if (!jsonObject.has("consumers")) {
            return dubboConsumerList;
        }
        JsonArray jsonElements = jsonObject.getAsJsonArray("consumers");
        for (JsonElement jsonElement : jsonElements) {
            String ipAddr = jsonElement.getAsJsonObject().get("address").getAsString();
            String appName = "未知应用";
            JsonElement temp = jsonElement.getAsJsonObject().get("application");
            if (!temp.isJsonNull()) {
                appName = temp.getAsString();
            }
            dubboConsumerList.add(new DubboConsumer(appName, ipAddr));
        }
        return dubboConsumerList;
    }
}
